<template>
    <view class="container">
        <!-- #ifdef APP -->
        <view class="loading-logo">
            <image class="loading-logo-image" src="@/static/logo.png"></image>
        </view>
        <app-version v-if="hasNewVersion" :isAutoCheck="true" :appId="getAppId()" @noNowBack="noNowBack"></app-version>
        <!-- #endif -->
    </view>
</template>

<script>
import { mapState, mapMutations, mapActions, mapGetters } from 'vuex'
import storage from "@/uni-sub-base/utils/storage.js"
import config from "@/config/config.js"
import { getCartTotal, getUserCarList } from "@/utils/cartUtil.js"
import { getProductDisplay, getInitWebInfo, getInitWebInfoUp } from "@/utils/appInit.js"
import { GetAppSetting } from "@/service/api/web.js"
import { GetBindingInfo } from "@/service/api/member.js"
import { getDomain, getBusinessld } from "@/utils/appUrls.js"
import appVersion from "@/uni-sub-base/module/version/app-version.vue"
import { getAppVersionForServer } from "@/uni-sub-base/module/version/util.js"
import { watch } from 'vue'
export default {
    components: {
        appVersion,
    },
    data() {
        return {
            hasNewVersion: false,
            goLogin: false,
            isFromRegister: false,
            isScanCodeEnter: false,
            invitUserPhone: "",
        }
    },
    onLoad(e) {
        console.log("config", config)
        if (e.isFromRegister) {
            this.isFromRegister = e.isFromRegister
        }
        if (e.isScanCodeEnter) {
            this.isScanCodeEnter = e.isScanCodeEnter
        }
        if (e.invitUserPhone) {
            this.invitUserPhone = e.invitUserPhone
        }
        let domain = getDomain()
        let Businessld = getBusinessld()
        if (Businessld) {
            if (this.networkType != "none") {
                getInitWebInfo(domain, this, Businessld)
            }
        } else {
            if (this.networkType != "none") {
                getInitWebInfo(domain, this)
            }
        }
        if (e) {
            this.goLogin = e.goLogin
        }
        let param = {
            title: '首次加载',
        }
        if (this.networkType != "none") {
            this.tackVisit(param)
            this.initTack()
        }
    },
    mounted() {
        if (this.networkType != "none") {
            uni.showLoading({
                title: "加载中",
            })
        }

        //一些配置项可以在这里请求并保存
        setTimeout((e) => {
            if (this.networkType != "none") {
                this.appInit()
            }
        }, 500)
    },
    computed: {
        ...mapState(["networkType"]),
    },
    methods: {
        ...mapMutations(["SET_AUTH", "SET_THEME_COLOR"]),
        getSetting() {
            console.log("getSetting")
            let path = "?ver=" + config.version + "&sysType=1" //1商城 2管理端
            GetAppSetting(path).then((res) => {
                console.log("GetAppSetting", res)
                uni.hideLoading()
                if (res.State == 1) {
                    // if (res.Data.SyetemType != 0) {
                    if (res.Data.SyetemType != 0) {
                        //跳转用户端
                        storage.setCache("DefaultSite", res.Data.DefaultSite) //用户端网址
                        // storage.setCache("DefaultSite", 'https://lftest.hsy884.com/m') //用户端网址
                        console.log("DefaultSite", res.Data.DefaultSite)
                        this.easyReLaunch("/pages/web/index")
                    } else {
                        if (this.getAppSet().h5TokenLogin) {
                            let token = storage.getCache("token")
                            if (!token) {
                                let url = 'https://' + this.getUseUrl().domain + '/m/home/page'
                                storage.setCache("DefaultSite", url)
                                this.easyReLaunch("/pages/web/index")
                            } else {
                                this.normalFn()
                            }
                        } else {
                            this.normalFn()
                        }
                    }

                } else {
                    this.easyToast(res.Msg)
                }
            })
        },
        normalFn() {
            //#ifdef APP
            let appId = this.getAppId()
            getAppVersionForServer(appId).then((res) => {
                if (res && res.hasNew) {
                    this.hasNewVersion = true
                } else {
                    this.appLoginInit()
                }
            })
            //#endif
            //#ifndef APP
            this.appLoginInit()
            //#endif

            uni.hideLoading()
        },
        appLoginInit() {
            //判断登录状态
            let token = storage.getCache("token")
            if (!token || token == "") {
                //未登录
                uni.hideLoading()
                let webInfo = storage.getCache("webInfo")
                if (webInfo) {
                    //默认登录页
                    let url
                    if (webInfo.IsVisitor && !this.goLogin) {
                        //有游客账号
                        console.log("有游客账号")
                        url = "/pages/index/index"
                    } else {
                        if (webInfo.AppLoginType == 0) {
                            console.log("单商户")
                            //单商户
                            url = "/pages/login/index"
                        }
                        if (webInfo.AppLoginType == 1) {
                            console.log("多商户商户")
                            //多商户商户
                            url = "/pages/login/shop-login"
                        }
                        //判断是否使用授权登录页
                        //#ifdef MP-WEIXIN
                        if (webInfo.AppLoginType == 2) {
                            url = "/pages/login/authorize-new"
                        }
                        //#endif
                    }
                    console.log("邀请扫码注册特殊处理", this.isFromRegister)

                    // 邀请扫码注册特殊处理
                    if (this.isFromRegister) {
                        console.log("邀请扫码注册特殊处理")
                        url = "/pages/login/register?isScanCodeEnter=" + this.isScanCodeEnter + "&invitUserPhone=" + this.invitUserPhone
                    }
                    setTimeout(() => {
                        uni.reLaunch({
                            url: url,
                        })
                    }, 500)
                } else {
                    let domain = getDomain()
                    let Businessld = getBusinessld()
                    if (Businessld) {
                        getInitWebInfo(domain, this, Businessld)
                    } else {
                        getInitWebInfo(domain, this)
                    }
                    setTimeout((e) => {
                        this.appInit()
                    }, 500)
                }
            } else {
                //已登录
                this.SET_AUTH(token)
                //已登录初始化需要获取的接口
                this.appLoginedInit()
                //TODO 这里需要待所有初始化都完成再执行
                uni.hideLoading()
            }
        },
        //获取基本配置
        appInit() {
            console.log("appInit")
            let Businessld = getBusinessld()
            let webInfo = storage.getCache("webInfo")
            if (webInfo) {
                if (!webInfo.WebDomain) {
                    webInfo.WebDomain = config.domain
                    storage.setCache("webInfo", webInfo)
                }
                if (Businessld) {
                    getInitWebInfoUp(webInfo.WebDomain, this, Businessld)
                } else {
                    getInitWebInfoUp(webInfo.WebDomain, this)
                }
                console.log("goNext111111111")
                this.goNext()
                return
            }
            let selectShop = storage.getCache("selectShop")
            if (selectShop) {
                getInitWebInfo(selectShop.MobileDomain, this, Businessld)
            } else {
                getInitWebInfo(config.domain, this, Businessld)
            }
            console.log("goNext2")
            setTimeout((e) => {
                this.goNext()
            }, 500)
        },
        //设置主题色
        setThemeColorFn(Style) {
            //主题色
            let themeColor = ""
            switch (Style) {
                case "green":
                    themeColor = "#2cd334"
                    break
                case "blue":
                    themeColor = "#4076fc"
                    break
                case "purple":
                    themeColor = "#ff547c"
                    break
                case "black":
                    themeColor = "#000000"
                    break
                case "lightOrange":
                    themeColor = "#feae58"
                    break
                case "red":
                    themeColor = "#eb1010"
                    break
                case "rose":
                    themeColor = "#725abe"
                    break
                case "gyellow":
                    themeColor = "#dba868"
                    break
                case "orange":
                    themeColor = "#fe5822"
                    break
                case "blackBlue":
                    themeColor = "#2c4068"
                    break
                case "zise":
                    themeColor = "#fdbc00"
                    break
                case "style12":
                    themeColor = "#f6b7bc"
                    break
                case "style13":
                    themeColor = "#2a7472"
                    break
                case "weixing":
                    themeColor = "#0ab483"
                    break
                default:
                    themeColor = this.easyGetColor("main")
                    break
            }
            storage.setCache("themeColor", themeColor)
            this.SET_THEME_COLOR(themeColor)
        },
        //已登录的初始化接口
        appLoginedInit() {
            getProductDisplay().then((res) => { })
            let webInfo = storage.getCache("webInfo")
            if (webInfo.IsForceBindingInfo) {
                GetBindingInfo().then((res) => {
                    if (res.Data && res.Data.InfoEditList.length > 0) {
                        uni.reLaunch({
                            url: "/pages_shop/packet/upgrade/information",
                        })
                    } else {
                        setTimeout((e) => {
                            uni.reLaunch({
                                url: "/pages/index/index",
                            })
                        }, 500)
                    }
                })
            } else {
                setTimeout((e) => {
                    uni.reLaunch({
                        url: "/pages/index/index",
                    })
                }, 500)
            }
        },
        //获取商家配置值
        getFunctionSetting() {
            let param = {
                ids: "14,30",
            }
            getFunctionSettingValue(param).then((res) => {
                if (res.State && res.Data) {
                    storage.setCache("businessFunctionSetting", res.Data)
                }
            })
        },
        goNext() {
            this.getSetting()
        },
        noNowBack() {
            this.hasNewVersion = false
            this.appLoginInit()
        },
    },
}
</script>

<style lang="scss">
.loading-logo {
    display: flex;
    margin-top: 300rpx;
    justify-content: center;
}

.loading-logo-image {
    width: 200rpx;
    height: 200rpx;
    border-radius: 50%;
}
</style>
